package com.power200.android.service;

import android.util.Log;

import com.power200.android.bean.Sprint;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuming on 10/12/15.
 */
public class SprintService {

    private static final String Path = "/master-life-spt.dat";

    private List<Sprint> sprintList = new ArrayList<Sprint>();

    private static SprintService sprintService = new SprintService();


    private FileService fileService = new FileService();

    private SprintService() {
    }

    public static SprintService getInstance() {
        return sprintService;
    }

    public List<Sprint> getSprints() {
        return sprintList;
    }

    public void createSprint(Sprint sprint) {
        sprintList.add(sprint);
        File file = fileService.openFile(Path);
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
            osw.write(sprint.toFileString() + "\n");
            osw.flush();
            osw.close();
        } catch (FileNotFoundException e) {
            Log.e("File", "FileNotFoundException", e);
        } catch (IOException e) {
            Log.e("File", "IOException", e);
        }
    }
}
